package spring.parsing;

import spring.factory.SingletonBeanRegistry;

import java.util.HashMap;
import java.util.Map;

public class XmlParserFactory {

    private static final Map<XmlNodeType, XmlParser> CACHE_MAP = new HashMap<>(XmlNodeType.values().length);

    private XmlParserFactory() {
    }

    public static XmlParserFactory getInstance() {
        return Factory.INSTANCE;
    }

    public XmlParser create(XmlNodeType type, SingletonBeanRegistry singletonBeanRegistry) {
        XmlParser parser;
        if (CACHE_MAP.containsKey(type)) {
            parser = CACHE_MAP.get(type);
        } else {
            switch (type) {
                case BEAN:
                    parser = new XmlBeanParser(singletonBeanRegistry);
                    break;
                case TX:
                    parser = new XmlTxParser(singletonBeanRegistry);
                    break;
                case AOP:
                    parser = new XmlAopParser(singletonBeanRegistry);
                    break;
                case BEANS:
                default:
                    parser = new XmlBeansParser(singletonBeanRegistry);
            }
            CACHE_MAP.put(type, parser);
        }
        return parser;
    }

    private static class Factory {
        private static final XmlParserFactory INSTANCE = new XmlParserFactory();
    }
}
